package pl.smath.expression;

/**
 * Klasa reprezentuje ulamek
 * 
 * @author klangner
 *
 */
public class FractionTerm extends Term{

	private Term numerator;
	private Term denominator;
	
	/**
	 * Construktor
	 * @param text
	 */
	public FractionTerm(Term num, Term denom) {
		super("/");
		
		this.numerator = num;
		this.denominator = denom;
	}
	
	
	/**
	 * @return tekst wyrażenia
	 */
	public Term copy(){
		Term term = new FractionTerm(numerator.copy(), denominator.copy());
		
		term.setNegative(isNegative());
		return term;
	}

	
	
	/**
	 * Pobranie dolnej czesci ulamka
	 */
	public Term getDenominator(){
		return denominator;
	}

	
	/**
	 * Pobranie gornej czesci ulamka
	 */
	public Term getNumerator(){
		return numerator;
	}
	
	
	/**
	 * Sprawdzenie czy 2 wyrazenia sa sobie równowazne.
	 * Czyli czy mozna postawic miedzy nimi znak = (w sensie matematycznym)
	 */
	public boolean isTheSame(Term term){
		
		if(term instanceof FractionTerm){
			FractionTerm ft = (FractionTerm)term;
			return numerator.isTheSame(ft.getNumerator()) && denominator.isTheSame(ft.getDenominator());
		}
		
		return false;
	}
	
	
	/**
	 * Zamien na string
	 */
	public String toString(){
		String output = "";
		
		if(isNegative())
			output = "-";
		
		if(numerator instanceof MultiTerm)
			output += "(" + numerator.toString() + ")";
		else
			output += numerator.toString();
		
		output += "/";

		if(denominator instanceof MultiTerm)
			output += "(" + denominator.toString() + ")";
		else
			output += denominator.toString();
		
		return output;

	}

}
